package com.wll.thread.demo;

import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.SQLException;
import java.util.List;


@SpringBootTest
public class HuToolDB {


    public static void main(String[] args) throws SQLException {

        String id = IdUtil.nanoId();
        System.out.println(id);

        update();

        System.out.println(">>>>>>>>>>>>>>>>>>>>>run over<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }


    public static void update() throws SQLException {
        List<Entity> result1 = Db.use().query("select * from mate_equipment_check_params where level = 1");
        for (Entity entity1 : result1) {
            String oldId1 = (String) entity1.get("id");
            String newId1 = IdUtil.fastSimpleUUID();
            Db.use().update(Entity.create().set("id", newId1), //修改的数据
                    Entity.create("mate_equipment_check_params").set("id", oldId1) //where条件
            );
            List<Entity> result2 = Db.use().query("select * from mate_equipment_check_params where level = 2 and pid = ?", oldId1);
//            System.out.println("1-2-3---old1---------" + oldId1);
//            System.out.println("1-2-3---newId1---------" + newId1);
            System.out.println(entity1.get("name").toString() + " 2 >>>>" + result2.size());
            Integer size = 0;
            for (Entity entity2 : result2) {
                String oldId2 = (String) entity2.get("id");
                String oldPId2 = (String) entity2.get("pid");
                String newId2 = IdUtil.fastSimpleUUID();
//                System.out.println("1-2-3-----oldId2-------" + oldPId2 + ">>>>>>>>>>" + oldId2);
//                System.out.println("1-2-3-----newId2-------" + newId1 + ">>>>>>>>>>" + newId2);
                Db.use().update(Entity.create().set("pid", newId1).set("id", newId2), //修改的数据
                        Entity.create("mate_equipment_check_params").set("id", oldId2) //where条件
                );
                List<Entity> result3 = Db.use().query("select * from mate_equipment_check_params where level = 3 and pid = ?", oldId2);
                size += result3.size();
                for (Entity entity3 : result3) {
                    String oldId3 = (String) entity3.get("id");
                    String oldPId3 = (String) entity3.get("pid");
                    String newId3 = IdUtil.fastSimpleUUID();
//                    System.out.println("1-2-3-------------oldId3------------------------" + oldPId3 + ">>>>>>>>>>" + oldId3);
//                    System.out.println("1-2-3-------------newId3------------------------" + newId2 + ">>>>>>>>>>" + newId3);
                    Db.use().update(Entity.create().set("pid", newId2).set("id", newId3), //修改的数据
                            Entity.create("mate_equipment_check_params").set("id", oldId3) //where条件
                    );
                }
            }
            System.out.println(entity1.get("name").toString() + " 3 >>>>" + size);
            System.out.println(entity1.get("name").toString() + " children >>>>" + (size + result2.size()));

        }
    }
}
